package org.qiyu.live.provider.sku;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * @Program: qiyu-live-app
 *
 * @Description:
 *
 * @Author: zx
 *
 * @Create: 2024-08-14 16:04
 */
public class CodeGenerator {
    public static void main(String[] args) {
        String url = "jdbc:mysql://qiyu.com:8808/qiyu-live-sku?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true";
        FastAutoGenerator.create(url, "root", "root")
                .globalConfig(builder -> {
                    builder.author("zx") // 设置作者
                            .disableOpenDir()
                            .dateType(DateType.ONLY_DATE)
                            .outputDir("D:\\develop\\learn\\imooc\\qiyu-live\\workspace\\qiyu-live-app\\qiyu-live-sku-provider\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.LONG;
                            }
                            if (typeCode == Types.INTEGER) {
                                // 自定义类型转换
                                return DbColumnType.LONG;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("org.qiyu.live.provider") // 设置父包名
                                .moduleName("sku") // 设置父包模块名
                                .pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\develop\\learn\\imooc\\qiyu-live\\workspace\\qiyu-live-app\\qiyu-live-sku-provider\\src\\main\\java")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude("t_sku_info")    // 设置需要生成的表名
                                .addInclude("t_category_info")
                                .addInclude("t_sku_order_info")
                                .addInclude("t_sku_stock_info")
                                .addInclude("t_anchor_shop_info")
                                .addTablePrefix("t_", "c_") // 设置过滤表前缀
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
